package fi.joensuu.mpm;

import java.io.InputStream;

import fi.joensuu.mpm.data.DBHelper;
import fi.joensuu.mpm.data.Place;
import fi.joensuu.mpm.geo.Geoprocessor;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Picture;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

/**
 * Activity displays description of given place
 *  
 */
public class PlaceDescription extends Activity {

	private Button backButton;
	private Button directionButton;	
	private ButtonListener buttonListener = new ButtonListener();
	
	private TextView placeName = null;
	private TextView placePhone = null;
	private TextView placeAddress = null;
	private TextView placeHours = null;
	private TextView placeDescription= null;
	private ImageView thePicture = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	try{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.placedescription);
    		backButton = (Button) findViewById(R.id.backToAttListButton);
    		backButton.setOnClickListener(buttonListener);
    		directionButton = (Button) findViewById(R.id.showDirectionsButton);
    		directionButton.setOnClickListener(buttonListener);
        
    		int placeId = AttractionList.getSelectedAttractionID();
    		Place place = DBHelper.getDBHelper(this).getPlace(placeId);    		
    		
    		placeName = (TextView) findViewById(R.id.PlaceNameTextView);
    		placeName.setText(place.getName()+" ("+Geoprocessor.getDistance(this, place)+" m)");
    		placeName.setTextSize((float)(placeName.getTextSize()*1.4));    		    		    	    		
    		placeAddress = (TextView) findViewById(R.id.AddressTextView);    		
    		placeAddress.setLines(3);
    		placeAddress.setText("Address:\n   "+place.getStreet()+",\n   "+place.getPostalCode()+" "+place.getCity());
    		placeHours = (TextView) findViewById(R.id.OpeningHoursTextView);
    		placeHours.setLines(2);
    		placeHours.setText("Opening hours:\n   "+place.getOpeningHours());    		
    		placePhone = (TextView) findViewById(R.id.PhoneNoTextView);
    		placePhone.setLines(2);
    		placePhone.setText("Phone number:\n   "+(new Integer(place.getTelephone()).toString()));
    		placeDescription= (TextView) findViewById(R.id.DescriptionTextView);
    		placeDescription.setText(place.getDescription());
    		thePicture = (ImageView) findViewById(R.id.thePicture);
    		InputStream in = getResources().getAssets().open(place.getPicture());
    		BitmapDrawable drawable  = new BitmapDrawable(in);
    		
    		// 		Bitmap picture = BitmapFactory.decodeStream(in);    		
    		
    		thePicture.setImageDrawable(drawable);    		
    		ScaleType scaleType = ScaleType.FIT_CENTER;
			thePicture.setScaleType(scaleType);
			//LayoutParams params = new LayoutParams(20, 30);
			//thePicture.setLayoutParams(params );
    		
    		
    	}catch(Throwable e){
    		Utils.displayStackTrace(e, this, 5);
    	}
    }

	class ButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			if (v.getId()==R.id.backToAttListButton){
				Navigation.goToAttractionList(v.getContext());
			}else if (v.getId()==R.id.showDirectionsButton){
				//TODO			
			}		
		}
		
	}
		   
}
